/**
* @file        LocSignalDeliverManager
* @brief       定位信号处理类（1）
* @version     1.0.0
* @author      Changlin.Jing 
* @date        2023/2/16 17:11
* @copyright   Copyright  2022 Langge Software Co., Ltd. All rights reserved.
*/
#include "LocSignalDeliverManager.hpp"
//#include "PositionEstimator/PositionEstimatorDeliver.hpp"

#include <utility>
BEGIN_SPACE_LANGE_VDR
/**
 * 定位信号源处理类
 * @param workState
 */
vdr::LocSignalDeliverManager::LocSignalDeliverManager(const WorkState &workState) :mSignalDeliverChain(nullptr)
        ,mDeliverBuilder(nullptr)
{
    Init(workState);
}

/**
 * 初始化：
 * 1、构造信号处理链路模板
 * @param workState
 */
void LocSignalDeliverManager::Init(const WorkState &workState) {
    createSignalDeliverChain(workState);
}

void LocSignalDeliverManager::UnInit() {

}
/**
 * 信号传播
 * @param posSignal
 */
void LocSignalDeliverManager::OnSignal(std::shared_ptr<I_LOC_SIGNAL> posSignal) {
    if(nullptr!=mSignalDeliverChain){
        mSignalDeliverChain->onSignal(std::move(posSignal));
    }
}
/**
 * 构造信号处理链路模板
 * @param workState
 */
void LocSignalDeliverManager::createSignalDeliverChain(const WorkState &workState) {
    destroySignalDeliverChain();
    mDeliverBuilder = LocSignalDeliverChainBuilderFactory::createDeliverChainBuilder(workState);
    if(nullptr == mDeliverBuilder){
        return;
    }
    mSignalDeliverChain = mDeliverBuilder->createLocSignalDeliverChain(workState);

}

/**
 * 退出处理链路
 */
void LocSignalDeliverManager::destroySignalDeliverChain() {
    if(nullptr!=mSignalDeliverChain){
        mSignalDeliverChain.reset();
    }
    if(nullptr!=mDeliverBuilder){
        mDeliverBuilder.reset();
    }

}

//void LocSignalDeliverManager::AddPosTimerObserver(vdr::IPosTimerObserver *posTimerObserver) {
//
//}

/**
 * 获取融合位置
 * todo::加接口获取原始位置或者增加扩展字段
 * @param time
 * @return
 */
std::shared_ptr<LOC_POS_SIGNAL> LocSignalDeliverManager::GetFusionSignal(uint64_t time) {
    auto deliver = mSignalDeliverChain;
    while(deliver!= nullptr){
        if(deliver->getSignalDeliverId() == SIGNAL_DELIVER_POSITION_ESTIMATOR){
            //            std::shared_ptr<vdr::PositionEstimatorDeliver>  positionEstimatorDeliver = std::static_pointer_cast<vdr::PositionEstimatorDeliver>(deliver);
            //            if(positionEstimatorDeliver!= nullptr){
            //                return positionEstimatorDeliver->GetFusionSignal(time);
            //            }
            return nullptr;
        }
        deliver = deliver->getNextDeliver();
    }
    return nullptr;
}


END_SPACE_LANGE_VDR